﻿Public Class Daccion

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String) As List(Of Eaccion)
        Dim accions As List(Of Eaccion) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_accion_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            accions = New List(Of Eaccion)
                            Dim accion As Eaccion = Nothing
                            Do While dr.Read
                                accion = New Eaccion( _
                                    dr.GetInt32(dr.GetOrdinal("idAccion")), _
                                    dr.GetString(dr.GetOrdinal("descAccion")), _
                                    dr.GetString(dr.GetOrdinal("estado")), _
                                    dr.GetInt32(dr.GetOrdinal("idUsuario")))
                                accions.Add(accion)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return accions
    End Function
    Public Shared Function SeleccionarTodoXEstado(ByVal descEmpresa As String) As List(Of Eaccion)
        Dim accions As List(Of Eaccion) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_accion_selXEstado", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            accions = New List(Of Eaccion)
                            Dim accion As Eaccion = Nothing
                            Do While dr.Read
                                accion = New Eaccion( _
                                    dr.GetInt32(dr.GetOrdinal("idAccion")), _
                                    dr.GetString(dr.GetOrdinal("descAccion")), _
                                    dr.GetString(dr.GetOrdinal("estado")), _
                                    dr.GetInt32(dr.GetOrdinal("idUsuario")))
                                accions.Add(accion)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return accions
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal accion As Eaccion) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_accion_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@descAccion", SqlDbType.VarChar, 55).Value = accion.descAccion
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = accion.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal accion As Eaccion) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_accion_edit", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idAccion", SqlDbType.Int).Value = accion.idAccion
                cmd.Parameters.Add("@descAccion", SqlDbType.VarChar, 55).Value = accion.descAccion
                cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = accion.estado
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = accion.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal accion As Eaccion) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_accion_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idAccion", SqlDbType.Int).Value = accion.idAccion
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = accion.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

End Class
